﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BaseZombie : MonoBehaviour
{
    private float mMoveSpeed = 20;
    private float HP = 5;
    private int mHurt = 1;
    private Animator mAnimator;    

    private void Start()
    {
        int random = Random.Range(0, 5);
        //确定僵尸的出场位置
        this.GetComponent<RectTransform>().anchoredPosition = GetStartPos(random);
        mAnimator = this.GetComponent<Animator>();
    }

    private void Update()
    {
        //移动僵尸
        this.transform.Translate(Vector3.left * Time.deltaTime * mMoveSpeed);
    }

    private Vector2 GetStartPos(int random)
    {
        Vector2 anchorPos = default(Vector2);
        switch (random)
        {
            case 0:
                anchorPos = new Vector2(320, 175);
                break;
            case 1:
                anchorPos = new Vector2(320, 85);
                break;
            case 2:
                anchorPos = new Vector2(320, 5);
                break;
            case 3:
                anchorPos = new Vector2(320, -75);
                break;
            case 4:
                anchorPos = new Vector2(320, -155);
                break;
            default:
                break;
        }

        return anchorPos;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Bullet")
        {
            collision.gameObject.GetComponent<Bullet>().Explosion();
            Destroy(collision.gameObject, 0.05f);
            this.HP--;
            if (HP <= 0)
            {
                //死亡动画
                mAnimator.SetBool("Dead", true);
                mMoveSpeed = 0;
                this.GetComponent<Collider2D>().enabled = false;
                this.transform.GetChild(0).gameObject.SetActive(true);
                Destroy(this.gameObject, 3.0f);
            }
        }
        if (collision.tag == "Trigger")
        {
            //游戏结束
            GameObject.Find("MainPanel").GetComponent<CanvasGroup>().interactable = false;
            //顯示結束面板
            GameObject.Find("EndPanel").GetComponent<CanvasGroup>().interactable = true;
            GameObject.Find("EndPanel").GetComponent<CanvasGroup>().blocksRaycasts = true;
            GameObject.Find("EndPanel").GetComponent<CanvasGroup>().alpha = 1;
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Plant")
        {
            mAnimator.SetTrigger("Attack");
            StartCoroutine(AttackPlant(collision.gameObject));
        }
    }

    private IEnumerator AttackPlant(GameObject plant)
    {
        while (true)
        {
            int hp = plant.GetComponent<BasePlant>().ReduceHP(mHurt);
            if (hp <= 0)
            {
                Destroy(plant);
                mAnimator.SetTrigger("Idle");
                break;
            }
            yield return new WaitForSeconds(1.5f);
        }
        yield return null;
    }
}
